const { ccclass, property } = cc._decorator;
import { ResourceItemBean } from "../bean/ResourceItemBean";

@ccclass
export default class RefreshTime extends cc.Component {
    private ui_countdown: cc.Node = null; //精灵所在节点
    private ui_countdown_particle: cc.Node = null; //粒子
    private particlePos: cc.Vec3 = null; //粒子开始坐标

    onLoad() {
        this.ui_countdown = this.node.getChildByName("ui_countdown");
        this.ui_countdown_particle = this.node.getChildByName("ui_countdown_particle");
    }

    /**
     * 开始倒计时
     * @param wait_sces 剩余倒计时时间
     * @param normal_sces 总倒计时时间
     */
    showCountdown(mapId: number, wait_sces: number = 30 * 1000, normal_sces?: number) {
        if (normal_sces == null || undefined) {
            normal_sces = wait_sces;
        }

        let bStartWithZero = wait_sces == normal_sces ? true : false;

        let timer = new Date().valueOf();
        let cur_step = bStartWithZero ? 0 : normal_sces - wait_sces;
        let all_step = normal_sces;
        let cur_step_temp = cur_step;

        let ui_countdown_comp = this.ui_countdown.getComponent(cc.Sprite);
        ui_countdown_comp.node.color = cc.color(0, 255, 0);
        ui_countdown_comp.fillStart = 0;
        ui_countdown_comp.fillRange = 1 - cur_step / all_step;

        // 更新逻辑
        let call_back = cc.callFunc(() => {
            cur_step = bStartWithZero ? new Date().valueOf() - timer : cur_step_temp + new Date().valueOf() - timer;
            if (cur_step > all_step) {
                ui_countdown_comp.node.stopAllActions();
                this.ui_countdown.active = false;
                this.ui_countdown_particle.active = false;
                ResourceItemBean.inst.updateResourceRefreshTimeCache(mapId, 0);
                return;
            }
            let step_fillrange = cur_step / all_step;
            ui_countdown_comp.fillRange = 1 - step_fillrange;

            let col = this.getColorByFillRange(ui_countdown_comp.fillRange);
            ui_countdown_comp.node.color = col;
            // this.refreshParticle(col);
        });

        // 动作执行
        let seq = cc.sequence(cc.delayTime(0.1), call_back);
        let repeater = cc.repeatForever(seq);
        ui_countdown_comp.node.active = true;
        ui_countdown_comp.node.runAction(repeater);
    }

    //颜色控制
    getColorByFillRange(fill_range: number) {
        let one = 500 / 60;
        let r = 0;
        let g = 0;
        let b = 0;

        if (fill_range < 0.33) {
            r = one * fill_range * 90;
            g = 255;
        } else if (fill_range < 0.66) {
            r = 255;
            g = 255 - (fill_range * 90 - 30) * one;
        } else {
            r = 255;
        }
        return cc.color(r, g, b);
    }

    //尾巴特效
    refreshParticle(color: any) {
        let particle: cc.ParticleSystem = this.ui_countdown_particle.getComponent(cc.ParticleSystem);
        particle.endColor = new cc.Color(color.getR(), color.getG(), color.getB(), 100);
        if (!this.ui_countdown_particle.active) {
            this.ui_countdown_particle.active = true;
            this.particlePos = this.ui_countdown_particle.position;
            particle.resetSystem();
        }

        let ui_countdown_comp = this.ui_countdown.getComponent(cc.Sprite);

        let W = this.ui_countdown.width / 2 - 2;
        let H = this.ui_countdown.height / 2 - 2;
        let A = Math.atan(H / W);

        let x = 0;
        let y = 0;

        //运动轨迹
        let PI = Math.PI;
        let cur_fill = 1 - ui_countdown_comp.fillRange;
        let a = PI * 2 * cur_fill;
        if (PI * 2 - A <= a || a <= A) {
            x = W;
            y = -W * Math.tan(a);
        } else if (A < a && a < PI - A) {
            x = H / Math.tan(a);
            y = -H;
        } else if (PI - A <= a && a <= PI + A) {
            x = -W;
            y = W * Math.tan(a);
        } else if (PI + A < a && a < PI * 2 - A) {
            x = -H / Math.tan(a);
            y = H;
        }

        this.ui_countdown_particle.x = this.ui_countdown.x + x - 1;
        this.ui_countdown_particle.y = this.ui_countdown.y + y;

        //自定义粒子效果--根据坐标变化 改变重力
        // particle.custom = true;
        // let _gravityX = this.particlePos.x - this.ui_countdown_particle.x;
        // let _gravityY = this.particlePos.y - this.ui_countdown_particle.y;
        // this.particlePos = this.ui_countdown_particle.position;
        // particle.gravity = cc.v2(_gravityX * 50, _gravityY * 50);
    }
}
